#include "gtest/gtest.h"
#include "../BaseUtil/MyTest.h"
#include "DOMCoreImp.h"
namespace DOMCoreImp {

class TextImpTest : public testing::Test {
protected:
    virtual void SetUp() {
    	doc = new DocumentImp();
    	text = doc->createTextNodeImp( "TextValue" );

    	elem = doc->createElementImp( "AnyElem" );
    }
    virtual void TearDown() {
    	delete doc;
    }

    DocumentImp * doc;
    TextImp * text;
    ElementImp * elem;
};

TEST_F(TextImpTest, Inheritance) {
	TextImpPtr( new TextImp("", NULL) );
	TextPtr( new TextImp("", NULL) );
	CharacterDataImpPtr( new TextImp("", NULL) );
	CharacterDataPtr( new TextImp("", NULL) );
}

TEST_F(TextImpTest, NodeType) {
	ASSERT_EQ( Node::TEXT_NODE, text->getNodeType() );
}

TEST_F(TextImpTest, SplitTextWithoutParent) {
	text->setData( "MyData" );
	Text * newText = text->splitText( 2 );
	ASSERT_TRUE( newText );

	ASSERT_STREQ( "My", text->getData().c_str() );
	ASSERT_STREQ( "Data", newText->getData().c_str() );

	ASSERT_EQ( doc, text->getOwnerDocument() );
	ASSERT_EQ( doc, newText->getOwnerDocument() );
}

TEST_F(TextImpTest, SplitTextWithParent) {
	elem->appendChild( text );
	ASSERT_EQ( 1ul, elem->getChildNodes()->getLength() );

	text->setData( "MyData" );
	Text * newText = text->splitText( 2 );
	ASSERT_TRUE( newText );

	ASSERT_STREQ( "My", text->getData().c_str() );
	ASSERT_STREQ( "Data", newText->getData().c_str() );

	ASSERT_EQ( elem, text->getParentNode() );
	ASSERT_EQ( elem, newText->getParentNode() );
	ASSERT_EQ( doc, text->getOwnerDocument() );
	ASSERT_EQ( doc, newText->getOwnerDocument() );

	ASSERT_EQ( 2ul, elem->getChildNodes()->getLength() );
}

TEST_F(TextImpTest, SplitTextThrowException) {
	text->setData( "12345" );
	ASSERT_THROW( text->splitText( 6 ), DOMExceptionPtr );
	text->splitText( 5 );
}


}
